package com.efast.dmom.mgmt.service.master.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.dao.master.TmBasPackageSnDao;
import com.efast.dmom.mgmt.entity.master.TmBasPackage;
import com.efast.dmom.mgmt.entity.master.TmBasPackageSn;
import com.efast.dmom.mgmt.service.master.TmBasPackageService;
import com.efast.dmom.mgmt.service.master.TmBasPackageSnService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
@Service
public class TmBasPackageSnServiceImpl implements TmBasPackageSnService{
    @Autowired
    private TmBasPackageSnDao tmBasPackageSnDao;
    @Qualifier("jdbcDao")
    @Autowired
	private JdbcDao jdbcDao;
    @Autowired
    private TmBasPackageService tmBasPackageService;
    
    @Override
    public long getTmBasPackageSnRowCount(Assist assist){
        return tmBasPackageSnDao.getTmBasPackageSnRowCount(assist);
    }
    @Override
    public List<TmBasPackageSn> selectTmBasPackageSn(Assist assist){
        return tmBasPackageSnDao.selectTmBasPackageSn(assist);
    }
    @Override
    public TmBasPackageSn selectTmBasPackageSnByObj(TmBasPackageSn obj){
        return tmBasPackageSnDao.selectTmBasPackageSnByObj(obj);
    }
    @Override
    public TmBasPackageSn selectTmBasPackageSnById(Integer id){
        return tmBasPackageSnDao.selectTmBasPackageSnById(id);
    }
    @Override
    public int insertTmBasPackageSn(TmBasPackageSn value){
        return tmBasPackageSnDao.insertTmBasPackageSn(value);
    }
    @Override
    public int insertNonEmptyTmBasPackageSn(TmBasPackageSn value){
        return tmBasPackageSnDao.insertNonEmptyTmBasPackageSn(value);
    }
    @Override
    public int insertTmBasPackageSnByBatch(List<TmBasPackageSn> value){
        return tmBasPackageSnDao.insertTmBasPackageSnByBatch(value);
    }
    @Override
    public int deleteTmBasPackageSnById(Integer id){
        return tmBasPackageSnDao.deleteTmBasPackageSnById(id);
    }
    @Override
    public int deleteTmBasPackageSn(Assist assist){
        return tmBasPackageSnDao.deleteTmBasPackageSn(assist);
    }
    @Override
    public int updateTmBasPackageSnById(TmBasPackageSn enti){
        return tmBasPackageSnDao.updateTmBasPackageSnById(enti);
    }
    @Override
    public int updateTmBasPackageSn(TmBasPackageSn value, Assist assist){
        return tmBasPackageSnDao.updateTmBasPackageSn(value,assist);
    }
    @Override
    public int updateNonEmptyTmBasPackageSnById(TmBasPackageSn enti){
        return tmBasPackageSnDao.updateNonEmptyTmBasPackageSnById(enti);
    }
    @Override
    public int updateNonEmptyTmBasPackageSn(TmBasPackageSn value, Assist assist){
        return tmBasPackageSnDao.updateNonEmptyTmBasPackageSn(value,assist);
    }

    public TmBasPackageSnDao getTmBasPackageSnDao() {
        return this.tmBasPackageSnDao;
    }

    public void setTmBasPackageSnDao(TmBasPackageSnDao tmBasPackageSnDao) {
        this.tmBasPackageSnDao = tmBasPackageSnDao;
    }
	@Override
	public void createPackageSn(int qty, Integer packageId) {
		String currentUser = SpringWebUtils.getRequestUser();
		String companyCode = SpringWebUtils.getRequestCompany();
		List<TmBasPackageSn> packageList = new ArrayList<TmBasPackageSn>();
		for(int i=0; i<qty; i++) {
			String ASNcode = jdbcDao.executeProcGetCode("tm_bas_box_sn", "efast");
			TmBasPackageSn packageSn = new TmBasPackageSn();
			packageSn.setPackageId(packageId);
			packageSn.setPackageSn(ASNcode);
			packageSn.setIsUsed(0);//是否已使用，0：否；1：是
			packageSn.setCreateTime(new Date());
			packageSn.setCreateUser(currentUser);
			packageSn.setCompanyCode(companyCode);
			packageSn.setOptCounter(0);
			packageList.add(packageSn);
		}
		tmBasPackageSnDao.insertTmBasPackageSnByBatch(packageList);
	}

	@Override
	public Map<String, Object> getBoxInfo(String packageSn) {
		Map<String,Object> objMap = new HashMap<String,Object>();
		Assist assist = new Assist();
		assist.setRequires(Assist.andEq("package_sn", packageSn));
		List<TmBasPackageSn> packageSnList = this.selectTmBasPackageSn(assist);
		if(packageSnList!=null&&packageSnList.size()>0) {
			TmBasPackageSn packageSnObj = packageSnList.get(0);
			objMap.put("hasSn", true);
			if(packageSnObj.getIsUsed().equals(0)) {//packageSn号箱子还未使用，可用
				objMap.put("available", true);
				TmBasPackage p = tmBasPackageService.selectTmBasPackageById(packageSnObj.getPackageId());
				objMap.put("standardQty", p.getQtyForPackage());
			}else {
				objMap.put("available", false);
			}
		}else {
			objMap.put("hasSn", false);
		}
		return objMap;
	}
	@Override
	public void updateIsInbound(String boxNumber) {
		tmBasPackageSnDao.updateIsInbound(boxNumber);	
	}
}